/**
 * @date 2015年12月15日 下午4:08:28
 * @version V1.0
 */
package Commons;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @ClassName: LDAResultsProcess
 * @Description: 用于读取或者写入LDA模型结果
 * @author ffftzh
 * @date 2015年12月15日 下午4:08:28
 *
 */
public class LDAFileProcess {
    HashMap<String, Integer> Word2Index = new HashMap<String, Integer>();
    HashMap<Integer, String> Index2Word = new HashMap<Integer, String>();
    ArrayList<ArrayList<String[]>> tassign = new ArrayList<ArrayList<String[]>>();
    ArrayList<ArrayList<String>> corpus = new ArrayList<ArrayList<String>>();


    public HashMap<String, Integer> getWord2Index() {
        return Word2Index;
    }

    public void setWord2Index(HashMap<String, Integer> word2Index) {
        Word2Index = word2Index;
    }

    public HashMap<Integer, String> getIndex2Word() {
        return Index2Word;
    }

    public void setIndex2Word(HashMap<Integer, String> index2Word) {
        Index2Word = index2Word;
    }

    public ArrayList<ArrayList<String[]>> getTassign() {
        return tassign;
    }

    public void setTassign(ArrayList<ArrayList<String[]>> tassign) {
        this.tassign = tassign;
    }

    public ArrayList<ArrayList<String>> getCorpus() {
        return corpus;
    }

    public void setCorpus(ArrayList<ArrayList<String>> corpus) {
        this.corpus = corpus;
    }

    public void read_wordMap(String wordMapPath) throws IOException {
        this.Word2Index = new HashMap<String, Integer>();
        this.Index2Word = new HashMap<Integer, String>();

        BufferedReader reader = IOUtil.getReader(wordMapPath);
        String tmpLine = null;
        String regex = "[\\s+|:]";
        while ((tmpLine = reader.readLine()) != null) {
            String[] sep_line = tmpLine.split(regex);
            if (sep_line.length != 2) {
                System.out.println("Warning:word_map encountering invalid input '" + tmpLine + "'");
            } else {
                this.Word2Index.put(sep_line[0], Integer.parseInt(sep_line[1]));
                this.Index2Word.put(Integer.parseInt(sep_line[1]), sep_line[0]);
            }
        }
        reader.close();
    }

    public void read_Tassign(String tassignPath) throws Exception {
        this.tassign = new ArrayList<ArrayList<String[]>>();

        BufferedReader reader = IOUtil.getReader(tassignPath);
        String tmpLine = null;
        String regex = "\\s+";
        while ((tmpLine = reader.readLine()) != null) {

            ArrayList<String[]> tassign_oneLine = new ArrayList<String[]>();
            String[] sep_line = tmpLine.split(regex);
            for (String item : sep_line) {
                String[] itemlist = item.split(":");
                if (itemlist.length != 2) {
                    throw new Exception("input error:" + item + ">>" + tmpLine);
                }
                tassign_oneLine.add(itemlist);
            }
            this.tassign.add(tassign_oneLine);
        }
        reader.close();
    }

    public void read_corpus(String tassignPath) throws Exception {
        this.tassign = new ArrayList<ArrayList<String[]>>();

        BufferedReader reader = IOUtil.getReader(tassignPath);
        String tmpLine = null;
        String regex = "\\s+";
        while ((tmpLine = reader.readLine()) != null) {

            ArrayList<String[]> tassign_oneLine = new ArrayList<String[]>();
            String[] sep_line = tmpLine.split(regex);
            for (String item : sep_line) {
                String[] itemlist = item.split(":");
                if (itemlist.length != 2) {
                    throw new Exception("input error:" + item + ">>" + tmpLine);
                }
                tassign_oneLine.add(itemlist);
            }
            this.tassign.add(tassign_oneLine);
        }
        reader.close();
    }

    public void write_corpus(String corpusPath, boolean ifHasHeadNum) throws Exception {

        BufferedWriter writer = IOUtil.getWriter(corpusPath);
        if (ifHasHeadNum) {
            writer.write(this.corpus.size() + "\n");
        }
        for (ArrayList<String> arrayList : this.corpus) {
            writer.write(utils.join(arrayList, " ") + "\n");
            writer.flush();
        }
        writer.close();
    }

    public static ArrayList<ArrayList<String>> read_topic_words(String path, int wordNum) throws IOException {
        ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
        BufferedReader reader = IOUtil.getReader(path);
        String oneLine = null;
        ArrayList<String> oneTopic = null;
        int count = -1;
        while ((oneLine = reader.readLine()) != null) {
            String[] itemList = oneLine.trim().split("\\s+");
            if (itemList.length != 2) {
                continue;
            }
            count++;
            if (itemList[0].equals("Topic") && count == 0) {
                oneTopic = new ArrayList<String>();
            } else if (oneTopic != null) {
                oneTopic.add(itemList[0]);
                if (count == wordNum) {
                    result.add(oneTopic);
                    oneTopic = null;
                    count = -1;
                }
            }
        }
        return result;
    }


}
